package com.szp.drug.component.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szp.drug.component.mapper.DrugMedicineMapper;
import com.szp.drug.component.service.api.DrugMedicineService;
import com.szp.drug.entity.DrugMedicine;
import com.szp.drug.util.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 药品实现类
 * @author SuZePing
 * @create 2021-03-05 22:00
 * @E-mail suzeping10@126.com
 */
@Slf4j
@Service
public class DrugMedicineServiceImpl extends ServiceImpl<DrugMedicineMapper, DrugMedicine> implements DrugMedicineService {

	@Autowired
	private DrugMedicineMapper medicineMapper;

	/**
	 * 分页获取药品
	 * @param medicineName
	 * @param medicineType
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResultEntity<PageInfo<DrugMedicine>> getPageInfo(String medicineName, String medicineType, String pageNum, String pageSize) {
		// 开启分页
		PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		QueryWrapper<DrugMedicine> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0")
				.and(m -> m.like("medicine_name",medicineName).like("medicine_type",medicineType))
				.orderByAsc("create_time");
		List<DrugMedicine> medicines = medicineMapper.selectList(wrapper);
		return ResultEntity.success(new PageInfo<>(medicines));
	}

	/**
	 * 新增药品
	 * @param medicine
	 * @return
	 */
	@Override
	public ResultEntity<Integer> saveMedicine(DrugMedicine medicine) {
		QueryWrapper<DrugMedicine> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0").eq("medicine_name",medicine.getMedicineName());
		List<DrugMedicine> selMedicines = medicineMapper.selectList(wrapper);
		if (selMedicines.size() != 0){
			return ResultEntity.error("药品名重复！");
		}
		medicine.setMedicineId("ME" + IdUtil.fastSimpleUUID().substring(0, 10))
				.setCreateTime(DateUtil.now());
		medicineMapper.insert(medicine);
		// 返回新增后总数量 为数据展示页码使用
		QueryWrapper<DrugMedicine> medicineWrapper = new QueryWrapper<>();
		medicineWrapper.eq("deleted","0");
		Integer count = medicineMapper.selectCount(medicineWrapper);
		return ResultEntity.success(count);
	}

	/**
	 * 修改药品
	 * @param medicine
	 * @return
	 */
	@Override
	public ResultEntity<String> editMedicine(DrugMedicine medicine) {
		String medicineId = medicine.getMedicineId();
		if (StrUtil.hasEmpty(medicineId)){
			return ResultEntity.error("主键id不能为空!");
		}
		String medicineName = medicine.getMedicineName();
		QueryWrapper<DrugMedicine> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0").eq("medicine_name",medicineName);
		List<DrugMedicine> selMedicines = medicineMapper.selectList(wrapper);
		if (selMedicines.size() != 0 && !Objects.equals(medicineId,selMedicines.get(0).getMedicineId())){
			return ResultEntity.error("药品名重复！");
		}
		medicineMapper.updateById(medicine);
		return ResultEntity.success();
	}

	/**
	 * 删除药品
	 * @param medicineId
	 * @return
	 */
	@Override
	public ResultEntity<String> removeMedicine(String medicineId) {
		if (StrUtil.hasEmpty(medicineId)){
			return ResultEntity.error("主键id不能为空!");
		}
		DrugMedicine medicine = new DrugMedicine();
		medicine.setMedicineId(medicineId).setDeleted("1");
		medicineMapper.updateById(medicine);
		return ResultEntity.success();
	}
}
